package edu.stu.mall.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import edu.stu.mall.databinding.ItemClassificationLayoutBinding
import edu.stu.mall.repository.data.ItemModel
import edu.stu.mall.service.loadImage

class HomeItemClassificationAdapter :
    RecyclerView.Adapter<HomeItemClassificationAdapter.HomeItemClassificationViewHolder>() {

    private var dataList: List<ItemModel> = mutableListOf()
    fun setData(list: List<ItemModel>?) {
        if (list != null && list.isNotEmpty()) {
            dataList = list
            notifyDataSetChanged()
        }
    }


    class HomeItemClassificationViewHolder(binding: ItemClassificationLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {
        var itemBinding: ItemClassificationLayoutBinding

        init {
            itemBinding = binding
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): HomeItemClassificationViewHolder {

        return HomeItemClassificationViewHolder(
            ItemClassificationLayoutBinding.inflate(
                LayoutInflater.from(parent.context),
                parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return dataList.size

    }

    override fun onBindViewHolder(holder: HomeItemClassificationViewHolder, position: Int) {
//        Glide.with(context).load(dataList[position].imageUrl).into(holder.itemBinding.imageView)

        loadImage(dataList[position].imageUrl, holder.itemBinding.imageView)
        holder.itemBinding.item = dataList[position]


    }
}